#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    init();
    connect(ui->treeWidget,SIGNAL(itemChanged(QTreeWidgetItem*,int)),this,
            SLOT(treeItemChanged(QTreeWidgetItem*,int)));
}

void Widget::init(){
    ui->treeWidget->clear();
    //第一个分组
    QTreeWidgetItem *group1 = new QTreeWidgetItem(ui->treeWidget);
    group1->setText(0,"group1");
    group1->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable);
    group1->setCheckState(0,Qt::Unchecked);

    QTreeWidgetItem *subitem11 = new QTreeWidgetItem(group1);
    subitem11->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable);
    subitem11->setText(0,"subitem11");
    subitem11->setCheckState(0,Qt::Unchecked);

    QTreeWidgetItem *subitem12 = new QTreeWidgetItem(group1);
    subitem12->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable);
    subitem12->setText(0,"subitem12");
    subitem12->setCheckState(0,Qt::Unchecked);

    QTreeWidgetItem *subitem13 = new QTreeWidgetItem(group1);
    subitem13->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable);
    subitem13->setText(0,"subitem13");
    subitem13->setCheckState(0,Qt::Unchecked);

    QTreeWidgetItem *subitem14 = new QTreeWidgetItem(group1);
    subitem14->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable);
    subitem14->setText(0,"subitem14");
    subitem14->setCheckState(0,Qt::Unchecked);

    //第二个分组
    QTreeWidgetItem *group2 = new QTreeWidgetItem(ui->treeWidget);
    group2->setText(0,"group2");
    group2->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable);
    group2->setCheckState(0,Qt::Unchecked);

    QTreeWidgetItem *subitem21 = new QTreeWidgetItem(group2);
    subitem21->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable);
    subitem21->setText(0,"subitem21");
    subitem21->setCheckState(0,Qt::Unchecked);

    QTreeWidgetItem *subitem22 = new QTreeWidgetItem(group2);
    subitem22->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable);
    subitem22->setText(0,"subitem12");
    subitem22->setCheckState(0,Qt::Unchecked);

    QTreeWidgetItem *subitem23 = new QTreeWidgetItem(group2);
    subitem23->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable);
    subitem23->setText(0,"subitem13");
    subitem23->setCheckState(0,Qt::Unchecked);
}

void Widget::treeItemChanged(QTreeWidgetItem *item, int column){
    QString itemText = item ->text(0);
    //选中时
    if(Qt::Checked == item->checkState(0)){
        QTreeWidgetItem *parent = item -> parent();
        int count = item->childCount();
        if(count>0){
            for(int i=0;i<count;i++){
                //子节点也选中
                item->child(i)->setCheckState(0,Qt::Checked);
            }
        }
        else{
            //是子节点
            updateParentItem(item);
        }
    }
    else if (Qt::Unchecked == item -> checkState(0)){
        int count = item -> childCount();
        if(count>0){
            for(int i=0;i<count;i++){
                item->child(i)->setCheckState(0,Qt::Unchecked);
            }
        }
        else{
            updateParentItem(item);
        }
    }
}
void Widget::updateParentItem(QTreeWidgetItem *item){
    QTreeWidgetItem *parent=item->parent();
    if(parent == NULL){
        return;
    }
    //选中的子节点个数
    int selectedCount = 0;
    int childCount = parent -> childCount();
    for(int i=0;i<childCount;i++){
        QTreeWidgetItem *childitem = parent->child(i);
        if(childitem -> checkState(0) == Qt::Checked){
            selectedCount++;
        }
    }
    if(selectedCount <= 0){
        //未选中状态
        parent -> setCheckState(0,Qt::Unchecked);
    }
    else if(selectedCount >0 && selectedCount < childCount){
        //部分选中状态
        parent -> setCheckState(0,Qt::PartiallyChecked);
    }
    else if(selectedCount == childCount){
        //选中状态
        parent->setCheckState(0,Qt::Checked);
    }
}

Widget::~Widget()
{
    delete ui;
}
